package main

import (
	"02-grpc/hello"
	"context"
	"log"
	"net"

	"google.golang.org/grpc"
)

type Server struct {
	hello.UnimplementedHelloRpcServer
}

func (s *Server) HelloWorld(ctx context.Context, req *hello.Hello) (*hello.Response, error) {
	log.Printf("received: %v", req.GetName())

	return &hello.Response{
		Reply: "hello " + req.GetName(),
	}, nil
}

func main() {
	// 注册服务
	g := grpc.NewServer()
	hello.RegisterHelloRpcServer(g, &Server{})
	lis, err := net.Listen("tcp", "0.0.0.0:8088")
	if err != nil {
		panic("failed to listen:" + err.Error())
	}
	// 启动服务
	err = g.Serve(lis)
	if err != nil {
		panic("failed to start grpc:" + err.Error())
	}
}
